package q.yazg.gen;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author khalisq
 */
public class NameGenerator {

    static Random rnd = new Random();
    
    private String[] a = new String[]{
        "a","e","o","u","i","ae","ai","ea","oa"
    };
    private String[] b = new String[]{
        "b","c","d","f","g","h","j","k","l","m","n","p","r","s","t","v","w","x","y","z",
        "br","by","ch","cr","dh","dr","dj","ds","dv","dw","ff","fr","gh","gr","gv",
        "hr","kf","kh","kr","ky","ld","lk","lm","ln","mn","mr","nd","ng","nn","ph","pr",
        "rb","rd","rh","rm","rn","rp","rr","rs","sh","sp","st","th","tr","vg","vh",
        "wh","ym","zd","zh","zr"
    };
    
    private final Map<Character, String[]> groups;
    
    public NameGenerator(JSONObject def) throws JSONException{
        groups = new HashMap<Character, String[]>(8,1.0f);
        Iterator i = def.keys();
        for(String key = (String) i.next();i.hasNext();key=(String) i.next()){
            if(key.length()==1){
                char c = key.charAt(0);
                JSONArray arr = def.getJSONArray(key);
                String[] val = new String[arr.length()];
                for(int j=0;j<arr.length();j++)val[j]=arr.getString(j);
                groups.put(c, val);
            } else if(key.equals("rule")) {
                
            }
        }
    }
    
    public String generate(){
        boolean nowA = rnd.nextBoolean();
        int length = rnd.nextInt(4)+3;
        StringBuilder ret = new StringBuilder();
        while(length>0){
            length--;
            String toAdd = null;
            if(nowA)toAdd=a[rnd.nextInt(a.length)];
            else toAdd=b[rnd.nextInt(b.length)];
            nowA ^= true;
            ret.append(toAdd);
        }
        return ret.toString();
    }
    
    class NameGroup{
        
        final char[] charGroups;
        final int    min,max;

        private NameGroup(char[] charGroups, int min, int max) {
            this.charGroups = charGroups;
            this.min = min;
            this.max = max;
        }  
        
    }
    
}
